<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * 递归无限级分类【先序遍历算】，获取任意节点下所有子孩子
     * @param array $arrCate 待排序的数组
     * @param int $parentId 父级节点
     * @param int $level 层级数
     * @param string $parentColumn 父级节点键名
     * @return array $arrTree 排序后的数组
     * @see http://blog.csdn.net/a272503680/article/details/8212189
     */
    function getMenuTree($arrCat, $parentId = 0, $level = 0, $parentColumn = 'p_id')
    {
        static  $arrTree = array(); //使用static代替global
        if(empty($arrCat)) return FALSE;
        $level++;
        foreach($arrCat as $key => $value)
        {
            if($value[$parentColumn] == $parentId)
            {
                $value['level'] = $level;
                $arrTree[] = $value;
                unset($arrCat[$key]); //注销当前节点数据，减少已无用的遍历
                $this->getMenuTree($arrCat, $value[ 'id'], $level);
            }
        }
        return $arrTree;
    }
}
